//Berechnung eines Zufallsignals

#include "tS.h"



void calcSignal(signal * s, sampling * samp, double * df0, double freq, double amp)
{
	//----------------------Initialisieren-----------------------------------//
	s->noiseamp = 3;					//ZUM EINGEBEN
	s->amp = amp;					    	//ZUM EINGEBEN
	s->length = 1000;					//ZUM EINGEBEN


	s->noiseToAmpRatio = s->noiseamp / s->amp;
	srand(time(NULL));
	int zufall = rand() % 100000;
	double zufall1 = (double) zufall /100000;
	s->phase = 0.5; //Phase bleibt konstant
	s->freq = freq;
	s->t = (double *)calloc(s->length, sizeof(double));
	double zaehler = (double) 1/(s->length-1);
	double zaehler2 = 0;
	for(int i = 0; i < s->length; i++)
	{
		s->t[i] = samp->tObservationTotal * zaehler2;
		zaehler2 += zaehler;
	}
	s->Im = (double *)calloc(s->length, sizeof(double));
	s->Re = (double *)calloc(s->length, sizeof(double));
	//----------------------Initialisieren-----------------------------------//
	
	for(int i = 0; i < s->length; i++)
	{
		double arg = (double) 2 * PI * s->freq * s->t[i] + s->phase;
		s->Re[i] = s->amp * cos(arg);
		s->Im[i] = s->amp * sin(arg);
		
		arg = 2 * PI*(rand()%10000)/10000;
		double x = 2*0.5*(rand()%1000)/1000;
		int y = rand()%2;
		//cout<<arg<<x<<y<<endl;		
		if(y)
		 y = 1;
		else
		 y = -1;
		//cout<<y<<"//"<<x<<"//"<<arg<<"//"<<cos(arg)<<"/// "<<s->noiseamp<<endl;
		//cout<< s->noiseamp * x * (double)y * cos(arg)<<endl;		
		s->Re[i] += s->noiseamp * x * (double)y * cos(arg);
		s->Im[i] += s->noiseamp * x * (double)y * sin(arg);
		//cout<<s->Im[i]<<endl;
	}
}
